package com.yjc.drygoods.ware.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjc.common.constant.OldTimersStatusEnum;
import com.yjc.drygoods.ware.entity.BatchesEntity;
import com.yjc.drygoods.ware.entity.ExchangeDetailEntity;
import com.yjc.drygoods.ware.entity.ExchangeEntity;
import com.yjc.drygoods.ware.entity.OldTimersEntity;
import com.yjc.drygoods.ware.exception.NotExitStockException;
import com.yjc.drygoods.ware.feign.ProductFeignService;
import com.yjc.drygoods.ware.service.BatchesService;
import com.yjc.drygoods.ware.service.ExchangeDetailService;
import com.yjc.drygoods.ware.service.ExchangeService;
import com.yjc.drygoods.ware.service.OldTimersService;
import com.yjc.drygoods.ware.to.SpuInfoTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 陈年老货表
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-10-26 15:02:13
 */
@Slf4j
@RestController
@RequestMapping("ware/oldtimers")
public class OldTimersController {
    @Autowired
    private OldTimersService oldTimersService;

    @Autowired
    ProductFeignService productFeignService;

    @Autowired
    private ExchangeDetailService exchangeDetailService;


    @GetMapping("/update/reducedPrice")
    public R updateReducedPrice(@RequestParam Long spuId, @RequestParam BigDecimal reducedPrice) {
        R r = productFeignService.updateReducedPrice(spuId, reducedPrice);
        OldTimersEntity oldTimersEntity = new OldTimersEntity();
        oldTimersEntity.setSpuId(spuId);
        if (r.getCode() != 0) {
            oldTimersEntity.setStatus(OldTimersStatusEnum.HAVE_EXCEPTION.getCode());
            oldTimersService.update(oldTimersEntity, new UpdateWrapper<OldTimersEntity>()
                    .eq("spu_id", spuId)
                    .ne("status", OldTimersStatusEnum.PROCESSED.getCode()));
            return R.error(r.getCode(), (String) r.get("msg"));
        }

        oldTimersEntity.setStatus(OldTimersStatusEnum.PROCESSED.getCode());
        oldTimersService.update(oldTimersEntity, new UpdateWrapper<OldTimersEntity>()
                .eq("spu_id", spuId)
                .ne("status", OldTimersStatusEnum.PROCESSED.getCode()));
        return R.ok();

    }

    /**
     * 加入换货需求
     *
     * @param params
     * @return
     */
    @GetMapping("/exchangedetail/save")
    public R addExchangeDetail(@RequestParam Map<String, Object> params) {
        ExchangeDetailEntity exchangeDetail = new ExchangeDetailEntity();
        exchangeDetail.setSpuId(Long.parseLong((String) params.get("spuId")));
        exchangeDetail.setWareId(Long.parseLong((String) params.get("wareId")));
        OldTimersEntity oldTimersEntity = new OldTimersEntity();
        oldTimersEntity.setId(Long.parseLong((String) params.get("id")));
        try {
            exchangeDetailService.saveExchangeDetail(exchangeDetail);
        } catch (Exception e) {
            log.info("进入了try...catch");
            e.printStackTrace();
            oldTimersEntity.setStatus(OldTimersStatusEnum.HAVE_EXCEPTION.getCode());
            oldTimersService.saveOrUpdate(oldTimersEntity, new UpdateWrapper<OldTimersEntity>().eq("id", Long.parseLong((String) params.get("id"))));
            return R.error(10000, e.getMessage());
        }
        oldTimersEntity.setStatus(OldTimersStatusEnum.PROCESSED.getCode());
        oldTimersService.saveOrUpdate(oldTimersEntity, new UpdateWrapper<OldTimersEntity>().eq("id", Long.parseLong((String) params.get("id"))));
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = oldTimersService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OldTimersEntity oldTimers = oldTimersService.getById(id);

        return R.ok().put("oldTimers", oldTimers);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OldTimersEntity oldTimers) {
        R r = productFeignService.spuInfo(oldTimers.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        SpuInfoTo data = r.getData("spuInfo", new TypeReference<SpuInfoTo>() {
        });
        oldTimers.setSpuName(data.getSpuName());
        oldTimers.setCreateTime(new Date());
        oldTimersService.save(oldTimers);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OldTimersEntity oldTimers) {
        R r = productFeignService.spuInfo(oldTimers.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        SpuInfoTo data = r.getData("spuInfo", new TypeReference<SpuInfoTo>() {
        });
        oldTimers.setSpuName(data.getSpuName());
        oldTimersService.updateById(oldTimers);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        oldTimersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
